package com.realphim.app;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.realphim.app.indie.videoplayer.VideoLoaderActivity;
import com.realphim.app.model.ModelPhim;
import com.realphim.app.util.CommonDefine;
import com.realphim.app.util.MyCache;

public class ActivityDetailsPhim extends Activity implements CommonDefine {

    private final String TAG = "ActivityDetailsPhim";

    private ProgressBar pbLoad;

    private Context _context;

    private ImageView ivBack;

    private ModelPhim currentPhim;

    private RelativeLayout rlCover;

    private TextView tvTitle, tvTitleTransparent;

    private TextView tvDirector, tvNation, tvType, tvActors, tvDuration,
            tvTags, tvDescription;

    private ImageView ivXemPhim, ivTrailer;

    private RelativeLayout rlXemPhim, rlTrailer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details_phim);
        _context = this;
        currentPhim = MyCache.getInstance().currentPhim;

        initView();
    }

    private void initView() {
        pbLoad = (ProgressBar) findViewById(R.id.pbload);

        ivBack = (ImageView) findViewById(R.id.ivBack);
        ivBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });

        //

        rlCover = (RelativeLayout) findViewById(R.id.rlCover);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitleTransparent = (TextView) findViewById(R.id.tvTitleTranslate);

        tvDirector = (TextView) findViewById(R.id.tvDirector);
        tvNation = (TextView) findViewById(R.id.tvNation);
        tvType = (TextView) findViewById(R.id.tvType);
        tvActors = (TextView) findViewById(R.id.tvActors);
        tvDuration = (TextView) findViewById(R.id.tvDuration);
        tvTags = (TextView) findViewById(R.id.tvTags);
        tvDescription = (TextView) findViewById(R.id.tvDescription);

        //

        tvTitle.setText(currentPhim.title);
        tvTitleTransparent.setText(currentPhim.titleTranslate);

        tvDirector.setText(currentPhim.director);
        tvNation.setText(currentPhim.nation);
        tvType.setText(currentPhim.type + "");
        tvActors.setText(currentPhim.actors);
        tvDuration.setText(currentPhim.duration + " phút");
        tvTags.setText(currentPhim.tag);
        tvDescription.setText(currentPhim.description);

        if (currentPhim.imageBitmap != null) {
            pbLoad.setVisibility(View.GONE);
            rlCover.setBackground(new BitmapDrawable(_context.getResources(),
                    currentPhim.imageBitmap));
        } else {
            new AsyncTaskLoadImage()
                    .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }

        //
        ivXemPhim = (ImageView) findViewById(R.id.ivXemPhim);
        ivTrailer = (ImageView) findViewById(R.id.ivTrailer);

        rlXemPhim = (RelativeLayout) findViewById(R.id.rlXemPhim);
        rlTrailer = (RelativeLayout) findViewById(R.id.rlTrailer);

        rlXemPhim.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(_context, VideoLoaderActivity.class);
                intent.putExtra("id", currentPhim.id);
                startActivity(intent);
            }
        });
    }

    private class AsyncTaskLoadImage extends AsyncTask<String, String, Bitmap> {

        @Override
        protected Bitmap doInBackground(String... params) {
            InputStream iStream = null;
            URL url;
            try {
                url = new URL(currentPhim.imageUrl);
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();
                urlConnection.connect();
                iStream = urlConnection.getInputStream();
                File cacheDirectory = getCacheDir();
                File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"
                        + currentPhim.title + "_feature.png");
                FileOutputStream fOutStream = new FileOutputStream(tmpFile);
                // Config option bitmap
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;

                options.inSampleSize = 2;
                options.inPurgeable = true;
                options.inInputShareable = true;
                options.inJustDecodeBounds = false;
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;

                Bitmap b = BitmapFactory.decodeStream(iStream, null, options);
                b.compress(Bitmap.CompressFormat.PNG, 100, fOutStream);

                // Bitmap result = Bitmap.createScaledBitmap(b, b.getWidth(),
                // b.getHeight(), true);
                // fOutStream.flush();
                // fOutStream.close();
                // b.recycle();
                return b;

            } catch (Exception e) {
                Log.e("LoadImage", e.toString());
                return null;
            }
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            pbLoad.setVisibility(View.GONE);
            if (result != null) {
                currentPhim.imageBitmap = result;
                rlCover.setBackground(new BitmapDrawable(_context
                        .getResources(), result));
            }
        }

    }

    private void finishActivity() {
        finish();
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
    }

    @Override
    public void onBackPressed() {
        finishActivity();
    }

}
